﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DslModeling = global::Microsoft.VisualStudio.Modeling;
using System.Drawing.Design;
using devtm.Editor.TypeSelector;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using devtm.Templates;


namespace devtm.AutoMapper
{
    public class ModelTemplateEditor : UITypeEditor
    {

        protected DslModeling.Store Store;
        protected Mapper Instance;

        /// <summary>
        /// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"/> method.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that can be used to gain additional context information.</param>
        /// <param name="provider">An <see cref="T:System.IServiceProvider"/> that this editor can use to obtain services.</param>
        /// <param name="value">The object to edit.</param>
        /// <returns>
        /// The new value of the object. If the value of the object has not changed, this should return the same object it was passed.
        /// </returns>
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {

            IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {

                Instance = (context.Instance as MapperShape).ModelElement as Mapper;
                Store = Instance.Store;

                System.Windows.Forms.ListBox control = new ListBox();
                control.DisplayMember = "Name";
                control.ValueMember = "Name";

                List<Type> lst = typeof(devtm.Templates.ITemplateModel).Assembly.GetTypes()
                    .Where(c => 
                        c.IsClass && 
                        !c.IsAbstract &&
                        c.IsOfType(false, typeof(ITemplateModel)                         
                        )
                ).ToList();

                List<Type> lst2 = new List<Type>();
                foreach (Type item in lst)
                {
                    ITemplateModel p = (ITemplateModel)Activator.CreateInstance(item);
                    if (p.IsEnumerable == Instance.MappingSource.TypeElement.IsEnumerable)
                        lst2.Add(item);
                }

                if (lst2.Count == 0)
                    return value;

                control.DataSource = lst2;

                control.VisibleChanged +=
                      (object sender, EventArgs e) =>
                      {
                          if (!String.IsNullOrEmpty((string)value))
                              control.SelectedValue = value;
                      };


                control.Click += (object sender, EventArgs e) =>
                {

                    if (sender != null && (sender as ListBox).SelectedItem != null)
                        try
                        {
                            value = (control.SelectedItem as Type).Name;
                        }
                        catch (Exception)
                        {

                        }

                    edSvc.CloseDropDown();

                };

                edSvc.DropDownControl(control);

            }

            return value;

        }



     

        public override bool IsDropDownResizable
        {
            get
            {
                return true;
            }
        }

        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
            //return base.GetEditStyle(context);
        }


    }
}
